package com.xxgc.quartzstudy.scheduler;

import com.xxgc.quartzstudy.job.ZhangSanJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 老板
 */
@Configuration
public class BossScheduler {

    /**
     * 安排事情
     * 起来重睡
     */
    @Bean
    public Scheduler sleepAgain() throws SchedulerException {
        //创建一个调度器工厂
        StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
        //获取一个调度器(老板)
        Scheduler scheduler = stdSchedulerFactory.getScheduler();
        //打工人
        JobDetail jobDetail = JobBuilder.newJob(ZhangSanJob.class)
                .storeDurably()
                .build();
        //闹钟
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
                .build();
        //老板去 > 关联Job（打工人）和触发器（闹钟）
        scheduler.scheduleJob(jobDetail,cronTrigger);
        //启动任务
        scheduler.start();
        return scheduler;
    }


}
